// 数组中两个字符串的最小距离
#include <iostream>
#include <climits>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    int index1 = -1, index2 = -1, ret = INT_MAX;
    string tmp;
    for(int i = 0; i < n; i++)
    {
        cin >> tmp;
        if(tmp == s1)
            index1 = i;
        else if(tmp == s2)
            index2 = i;
        if(index1 >= 0 && index2 >=0)
            ret = min(ret, abs(index1 - index2));
    }
    cout << (ret == INT_MAX ? -1 : ret) << endl;
    return 0;
}

// 简写单词
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    while(cin >> s)
    {
        if(s[0] >= 'a' && s[0] <= 'z')
            cout << (char)(s[0] - 32);
        else
            cout << s[0];
    }
    return 0;
}


// dd爱框框
#include <iostream>

using namespace std;

const int N = 1e7 + 10;
int x, n;
int arr[N];
int main()
{
    cin >> n >> x;
    for(int i = 1; i <= n; i++) cin >> arr[i];
    int retleft = -1, retright = -1 ,len = N;
    int left = 0, right = 0, sum = 0;
    while(right <= n)
    {
        sum += arr[right];
        while(sum >= x)
        {
            if(right - left + 1 < len)
            {
                retleft = left;
                retright = right;
                len = right - left + 1;
            }
            sum -= arr[left++];
        }
        ++right;
    }
    cout << retleft <<  " " << retright << endl;
    return 0;
}


// 除2！
#include <iostream>
#include <queue>
using namespace std;

const int N = 1e7 + 10;
int n, k;

int main()
{
    cin >> n >> k;
    priority_queue<long long> heap;
    long long x, ret = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> x;
        if(x % 2 == 0)
            heap.push(x);
        else
            ret += x;
    }
    
    while(k-- && !heap.empty())
    {
        x = heap.top();
        heap.pop();
        x /= 2;
        if(x % 2 == 0)
            heap.push(x);
        else
            ret += x;
    }
    while(heap.size())
    {
        ret += heap.top();
        heap.pop();
    }
    cout << ret << endl;
    
    return 0;
}


// Fibonacci数列
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    int f0 = 0, f1 = 1, f2 = 1;
    while(f2 < n)
    {
        f0 = f1;
        f1 = f2;
        f2 = f0 + f1;
    }
    cout << min((f2 - n), (n - f1));
    return 0;
}

// 单词搜索
#include <string>
class Solution {
  public:
    bool vis[101][101];
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    int m, n;
    bool exist(vector<string>& board, string word) {
        m = board.size(), n = board[0].size();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == word[0]) {
                    if (dfs(board, i, j, word, 0)) return true;
                }
            }
        }

        return false;
    }
    bool dfs(vector<string>& board, int i, int j, string& word, int pos) {
        if (pos == word.size() - 1) return true;

        vis[i][j] = true;
        for (int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] &&
                    board[x][y] == word[pos + 1])
                if (dfs(board, x, y, word, pos + 1)) return true;
        }
        vis[i][j] = false;
        return false;
    }
};


// 杨辉三角
#include <iostream>
using namespace std;
int dp[31][31];
int main() {
    int n;
    cin >> n;
    dp[1][1] = 1;
    for (int i = 2; i <= n; i++)
        for (int j = 1; j <= i; j++)
            dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%5d", dp[i][j]);
        }
        printf("\n");
    }
    return 0;
}